package toubiao.utils;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import toubiao.dao.BaseDaoI;
import toubiao.dao.business.DepartmentDaoI;
import toubiao.dao.business.ProjectDaoI;
import toubiao.dao.business.ProjectSnDaoI;
import toubiao.domain.excel.ProjectGrid4Excel;
import toubiao.domain.po.business.PoContact;
import toubiao.domain.po.business.PoContract;
import toubiao.domain.po.business.PoContractPhoto;
import toubiao.domain.po.business.PoDepartment;
import toubiao.domain.po.business.PoDesigner;
import toubiao.domain.po.business.PoPartA;
import toubiao.domain.po.business.PoProject;
import toubiao.domain.po.business.PoProjectSn;
import toubiao.domain.vo.business.ContractPhoto;
import toubiao.domain.vo.business.ProjectDetail;
import toubiao.domain.vo.business.ProjectGrid;
import toubiao.exception.CustomException;
import toubiao.factory.FactoryDao;
import utils.UtilString;

@Component
public class ProUtilVoConvert {

	@Autowired
	 private DepartmentDaoI departmentDao;
	
	@Autowired
	private FactoryDao factoryDao;
	
	@Autowired
	private ProjectSnDaoI projectSnDao;
	
	@Autowired
	private ProjectDaoI projectDao;
	
	public <S,T> T convert(Class<S> sourceClass,Class<T> targetClass,S s) throws CustomException{
		if(s==null){
			return null;
		}
		T t=null;
		
		// PoProject 转换
		if(targetClass.getSimpleName().equalsIgnoreCase("PoContractPhoto")){
			PoContractPhoto targetBeanFlag=new PoContractPhoto();
			if(sourceClass.getSimpleName().equalsIgnoreCase("ContractPhoto")){
				ContractPhoto sourceBeanFlag=new ContractPhoto();
				ContractPhoto source=(ContractPhoto) s;
				t=(T) getPo(sourceBeanFlag,targetBeanFlag,source);
				return t;
			}
		}
		
		throw new CustomException("未定义 从 "+sourceClass.getSimpleName()+" 到 "+targetClass.getSimpleName()+"的转换");
			
	}
	
	private  PoContractPhoto getPo(ContractPhoto sourceBeanFlag,
			PoContractPhoto targetBeanFlag, ContractPhoto source) throws CustomException {
		// TODO Auto-generated method stub
		PoContractPhoto po=null;
		Long contractId=source.getContractId();
		Long id=source.getId();
		if(id==null){
			po=new PoContractPhoto();
		}else{
			po=getPoById(PoContractPhoto.class, id);
			if(po==null){
				throw new CustomException("contractPhoto.id 错误,无法根据该id 找到对应的实体bean");
			}
		}
		PoContract contract=getPoById(PoContract.class, contractId);
		
		BeanUtils.copyProperties(source, po);
		po.setContract(contract);
		
		return po;
	}

	public PoProject getProject(ProjectDetail projectDetail,PoProject project){
		if(projectDetail==null)
			return null;
		if(project==null){
			project=new PoProject();
		}
		BeanUtils.copyProperties(projectDetail, project);
		
	
		project=updateProjectSn(project, projectDetail.getSns());
		
		String departmentIds=projectDetail.getDepartmentIds();
		Set<PoDepartment> departmentSet=getPoSetByIds(PoDepartment.class, departmentIds);
		project.setDepartmentSet(departmentSet);
		
		Set<PoDesigner> designerSet=getPoSetByIds(PoDesigner.class, projectDetail.getDesignerIds());
		project.setDesignerSet(designerSet);
		
		PoPartA partA=getPoById(PoPartA.class, projectDetail.getPartAId());
		project.setPartA(partA);
		
		Set<PoContact> contactSet=getPoSetByIds(PoContact.class, projectDetail.getContactIds());
		project.setContactSet(contactSet);
		
		return project;
	}

	
	public <T> Set<T> getPoSetByIds(Class<T> clazz,String ids){
		if(clazz==null || UtilString.isNullOrEmpty(ids)){
			return null;
		}
		
		List<Long> idList=UtilString.string2LongList(ids, ",");
		Set<T> set=new HashSet<>();
		for(Long id:idList){
			BaseDaoI<T> dao=factoryDao.getDaoByPoClass(clazz);
			T t=dao.get(clazz, id);
			set.add(t);
		}
		return set;
	}
	
	public <T> T getPoById(Class<T> clazz,Long id){
		if(clazz==null || id==null){
			return null;
		}
		BaseDaoI<T> dao=factoryDao.getDaoByPoClass(clazz);
		T t=dao.get(clazz, id);
		return t;
	}
	
	
	public PoProject updateProjectSn(PoProject project,String sns){
		Long id=project.getId();
		//id 为空的话  为新建 ,可以直接增加sn即可;
		//不为空的话 为更新:需要删除原来的 sn
		if(id!=null){
			Set<PoProjectSn> snSet=project.getSnSet();
			if(snSet==null){
				project=projectDao.get(PoProject.class, id);
				snSet=project.getSnSet();
			}
			if(snSet!=null){
				for(PoProjectSn sn:snSet){
					projectSnDao.delete(sn);
				}
			}
		}
		
		if(UtilString.isNullOrEmpty(sns)){
			project.setSnSet(null);
		}else{
			List<Integer> snIntList=UtilString.string2IntegerList(sns, ",");
			Collections.sort(snIntList);
			int seq=0;
			for(Integer snInt:snIntList){
				PoProjectSn projectSn=new PoProjectSn();
				projectSn.setSnInt(snInt);
				projectSn.setProject(project);
				projectSn.setSeq(seq);
				seq++;
				projectSnDao.save(projectSn);
			}
		}
		return project;
	}
	
	
	public  Set<PoDepartment> getDepartmentByIds(String ids) {
		// TODO Auto-generated method stub
		if(UtilString.isNullOrEmpty(ids)){
			return null;
		}
		List<Long> idList=UtilString.string2LongList(ids, ",");
		
		Set<PoDepartment> departmentSet=new HashSet<>();
		for(Long id:idList){
			PoDepartment department=departmentDao.get(PoDepartment.class, id);
			departmentSet.add(department);
		}
		return departmentSet;
	}
	
}
